package com.example.blue;

import android.accessibilityservice.FingerprintGestureController;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class utils {
    public static String TAG = "utils";
    public static String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            // & 0xFF 把负数转换成无符号值
            sb.append(String.format("%02x", b & 0xFF));
        }
        return sb.toString();
    }
    public static void writeToFile(String data, String fileName) throws IOException {
        File file = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOCUMENTS), fileName);
        Log.d(TAG, "Trying wrote to " + file.getAbsolutePath());
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            fos.write(data.getBytes());
            Log.d(TAG, "Successfully wrote to " + fileName);
        } catch (IOException e) {
            Log.e(TAG, "Failed to write to file", e);
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    Log.e(TAG, "Failed to close file stream", e);
                }
            }
        }
    }
    public static void copyAssetToFile(Context context, String assetFileName, File targetFile) {
        try (InputStream inputStream = context.getAssets().open(assetFileName);
             FileOutputStream outputStream = new FileOutputStream(targetFile)) {

            byte[] buffer = new byte[1024];
            int readBytes;
            while ((readBytes = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, readBytes);
            }
            outputStream.flush();
            Log.d(TAG, "File copied successfully from assets to: " + targetFile.getAbsolutePath());

        } catch (IOException e) {
            Log.e(TAG, "Failed to copy file: " + assetFileName, e);
        }
    }
    public static String resChange(Integer index){
        String res="";
        switch (index){
            case 1:
                res = "手腕";
                break;
            case 2:
                res= "大拇指";
                break;
            case 3:
                res= "食指";
                break;
            case 4:
                res =  "中指";
                break;
            case 5:
                res= "无名指";
                break;
            case 6:
                res= "小指";
                break;
            default:
                res = "手腕";
                break;
        }
        return res;
    }
    public static String zhenchanLabelChange(Integer index){
        String res="";
        switch (index){
            case -1:
                res = "疑似特发性震颤患者";
                break;
            case 1:
                res = "疑似帕金森病患者";
                break;
            default:
                res="";
                break;
        }
        return res;
    }
    public static String duizhiLabelChange(Integer index){
        String res="";
        switch (index){
            case -1:
                res = "正常";
                break;
            case 1:
                res = "疑似帕金森病患者";
                break;
            default:
                res="";
                break;
        }
        return res;
    }

}
